package org.zjx.core.state.domain;

import org.zjx.core.state.StateComponent;

import java.util.HashMap;
import java.util.Map;

/**
 * 玩家物品状态
 */
public class InventoryState implements StateComponent {
    private final Map<String, Integer> items = new HashMap<>();


    @Override
    public void reset() {
        items.clear();
    }

    @Override
    public StateComponent copy() {
        InventoryState copy = new InventoryState();
        copy.items.putAll(this.items);
        return copy;
    }

    public void addItem(String itemId, int count) {
        items.merge(itemId, count, Integer::sum);
    }

    public boolean useItem(String itemId) {
        Integer count = items.get(itemId);
        if (count == null || count <= 0) {
            return false;
        }
        items.put(itemId, count - 1);
        return true;
    }

    public int getItemCount(String itemId) {
        return items.getOrDefault(itemId, 0);
    }
}